from views.base import BaseHandler
from db import db
from util.myredis import r
from elasticsearch import Elasticsearch
es = Elasticsearch("http://101.43.203.36:9200/")
class Findes(BaseHandler):
    def get(self):
        # try:
        sql = "select * from landmarks1 where status=1 union all select * from landmarks2 where status=1 union all select * from landmarks where status=1"
        res = db.findall(sql)
        print(res)
        if res:
            for i in res:
                es.index(index="landmarks", doc_type="KK",body={
                    # 'table_name':'landmarks',
                    'code':i['code'],
                    'descrip':i['descrip'],
                    'purpose':i['purpose'],
                    'tmoney':i['tmoney'],
                    'pay_type':i['pay_type'],
                    'interest_rate':i['interest_rate'],
                    'term':i['term'],
                    'userid':i['userid'],
                    'status':i['status'],
                    'audit_userid':i['audit_userid'],
                    'audit_time':i['audit_time'],
                    'money':i['money'],
                    'due_time':i['due_time']
                })
                print("###")
            return 'ok'
        print("cuo")
        return '错误'

def updatedata():
    key = 'eslastdata'
    v = r.str_get(key)
    if v:
        sql = "select * from landmarks1 where status=1 union all select * from landmarks2 where status=1 union all select * from landmarks where status=1 where code>order by code asc"
    else:
        sql = "select * from landmarks1 where status=1 union all select * from landmarks2 where status=1 union all select * from landmarks where status=1 order by code asc"
    res = db.update(sql)
    db.commit()
    r.set(key,res[-1]['code'])

class Getlandmarkses(BaseHandler):
    def get(self):
        page = self.get_query_argument('page')
        purpose = self.get_query_argument('biao')
        pagesize= 2
        start = (int(page)-1)*pagesize
        dsl = {
            "query":{
                "match":{'purpose':purpose}
            },
            "from":start,
            "size":pagesize,
            "sort":{
                "code":{
                    'order':'desc'
                }
            }
        }
        dss = {
            "query":{
                "match":{'purpose':purpose}
            }
        }
        res = es.search(index='landmarks',body=dsl)
        count = es.count(index='landmarks',body=dss)
        cc= count['count']
        return self.finish({'code':200,'list':res['hits']['hits'],'count':cc})

class Getes(BaseHandler):
    def get(self):
        page = self.get_query_argument("page")
        pagesize = 2
        start = (int(page) - 1) * pagesize
        sortid = self.get_query_argument("sortid")
        type1 = self.get_query_argument("type")
        tagid = self.get_query_argument("tagid")
        online = self.get_query_argument("online")
        str = '"match_all":{}'
        flag = False
        ss = '"bool":{"must":[{"match":{"'
        if type1:
            flag = True
            ss = ss + 'type":' + type1 + '}}'
        if online:
            if flag == True:
                ss = ss + ',{"match":{"online":' + online + '}}'
            else:
                flag = True
                ss = ss + 'online":' + online + '}}'
        if tagid:
            if flag == True:
                ss = ss + ',{"match":{"tagid":' + tagid + '}}'
            else:
                flag = True
                ss = ss + 'tagid":' + tagid + '}}'

        if flag == True:
            str = ss + ']}'

        if sortid:
            sort = '"sort":{"id":{"order":"desc"}}'
            dsl = '{' + '"query":{' + str + '},"from":' + str(start) + ',"size":' + str(pagesize) + ',' + sort + '}'
        else:
            dsl = '{' + '"query":{' + str + '},"from":0,"size":20}'

        match_data = es.search(index="landmarks", body=dsl)
        results = []
        for hit in match_data['hits']['hits']:
            results.append(hit['_source'])
        return self.finish({"res": results})